package managedbean;

import entity.FnBBranchEntity;
import entity.FoodTypeEntity;
import entity.MealEntity;
import entity.ProductEntity;
import entity.SetMealEntity;
import entity.StakeholderEntity;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import javax.faces.model.SelectItem;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.event.SelectEvent;
import session.stateful.MealSessionBean;
import session.stateful.ProductSessionBean;
import exception.DeleteProductException;
import entity.FnBBranchEntity;
import entity.FnBProductEntity;
import entity.MealEntity;
import entity.PriceControlEntity;
import entity.ProductAvailabilityEntity;
import entity.ProductEntity;
import entity.SetMealEntity;
import entity.StakeholderEntity;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.event.RowEditEvent;
import org.primefaces.event.SelectEvent;
import session.stateful.ProductSessionBean;
import exception.PriceControlException;

/**
 *
 * @author TrungHieu-Henry
 */
@ManagedBean(name = "mealManagerBean")
@SessionScoped
public class MealManagerBean {

    @EJB
    private MealSessionBean mealSessionBean;
    @EJB
    private ProductSessionBean productSessionBean;
    private String name;
    private List<MealEntity> mealList;
    private List<SetMealEntity> setMealList;
    private MealEntity selectedMeal;
    private MealEntity[] selectedMeals;
    private MealEntity[] selectedMeals_update;
    private SetMealEntity selectedSetMeal;
    private Long option;
    private List options = new ArrayList();
    private List<FoodTypeEntity> foodTypes = new ArrayList<FoodTypeEntity>();
    private boolean reload = true;
    private boolean reloadAddSet = true;
    private boolean reloadAddMeal = true;
    private boolean reloadUpdateSetMeal;
    private String statusMessage;
    private FnBBranchEntity selectedBranch;
    private StakeholderEntity selectedStakeholder;
    private List<MealEntity> mealOfBranch;
    //ProductManagerBean
    private ProductEntity selectedProduct;
    private PriceControlEntity selectedPriceControl = new PriceControlEntity();
    private PriceControlEntity priceControl = new PriceControlEntity();
    private List<PriceControlEntity> listOfPriceControl = new ArrayList<PriceControlEntity>();
    private List dayOfWeek = new ArrayList();
    private List timeOfDay = new ArrayList();
    private boolean reloadPriceControl;
    private boolean reloadProductAvail;
    private ProductAvailabilityEntity[] selectedAvail;
    private List<ProductAvailabilityEntity> listOfAvail;
    private long productId;
    private FnBBranchEntity branch;
    private List<FnBProductEntity> listOfProduct;
    private boolean load = true;
    private FacesContext ctx = FacesContext.getCurrentInstance();

    public MealManagerBean() {
        selectedMeal = new MealEntity();
        selectedSetMeal = new SetMealEntity();
        reloadUpdateSetMeal = true;

        //ProductManagerBean
        reloadPriceControl = true;
        reloadProductAvail = true;
        dayOfWeek.add(new SelectItem(0, "Default"));
        dayOfWeek.add(new SelectItem(1, "Sunday"));
        dayOfWeek.add(new SelectItem(2, "Monday"));
        dayOfWeek.add(new SelectItem(3, "Tuesday"));
        dayOfWeek.add(new SelectItem(4, "Wednesday"));
        dayOfWeek.add(new SelectItem(5, "Thursday"));
        dayOfWeek.add(new SelectItem(6, "Friday"));
        dayOfWeek.add(new SelectItem(7, "Saturday"));
        dayOfWeek.add(new SelectItem(8, "Specific Date"));

        timeOfDay.add(new SelectItem(0, "12AM"));

        for (int i = 1; i <= 11; i++) {
            timeOfDay.add(new SelectItem(i, i + "AM"));
        }

        timeOfDay.add(new SelectItem(12, "12PM"));
        for (int i = 1; i <= 11; i++) {
            timeOfDay.add(new SelectItem(i + 12, i + "PM"));
        }
        timeOfDay.add(new SelectItem(24, "Midnight"));
    }

    public void initAddMeal(PhaseEvent event) throws IOException {
        if (!checkBranch()) {
            return;
        }
        if (reloadAddMeal) {
            selectedMeal = new MealEntity();
            reloadAddMeal = false;
        }
        if (foodTypes.isEmpty()) {
            foodTypes = productSessionBean.getFoodType();
            for (int i = 0; i < getFoodTypes().size(); i++) {
                options.add(new SelectItem(getFoodTypes().get(i).getId(), getFoodTypes().get(i).getName()));
            }
            setReload(false);
        }
    }

    public void initUpdateSetMeal(PhaseEvent event) throws IOException {
        System.out.println("initUpdateSetMeal");
        if (reloadUpdateSetMeal) {
            if (!checkProduct()) {
                return;
            }
            mealOfBranch = mealSessionBean.getMealListOfABranch(selectedBranch.getId());
            selectedMeals_update = mealSessionBean.getMealBySetMealId(selectedSetMeal.getId());
            reloadUpdateSetMeal = false;
        }
        if (selectedSetMeal == null) {
            if (!setMealList.isEmpty()) {
                selectedSetMeal = setMealList.get(0);
            } else {
                selectedSetMeal = new SetMealEntity();
            }
        }
        initPriceControl();
    }

    public void initUpdateMeal(PhaseEvent event) throws IOException {
        System.out.println("initUpdateMeal");
        reload = true;
        if (selectedMeal == null) {
            if (!mealList.isEmpty()) {
                selectedMeal = mealList.get(0);
            } else {
                selectedMeal = new MealEntity();
            }
        }
        if (!checkProduct()) {
            return;
        }
        initPriceControl();
    }

    public void saveMeal(ActionEvent event) {
        mealSessionBean.addMeal(selectedMeal, option, selectedBranch.getId());
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Add New Meal Result: successful! (New meal is " + selectedMeal.getName() + ")", ""));
        selectedMeal = new MealEntity();
    }

    public void saveSetMeal(ActionEvent event) {
        mealSessionBean.addSetMeal(selectedSetMeal, selectedMeals, selectedBranch.getId());
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Add New Meal Result: successful! (New Set meal is " + selectedSetMeal.getName() + ")", ""));
        reloadAddSet = true;
    }

    //upload
    public void handleImageUpload(FileUploadEvent event) throws AbortProcessingException, NoSuchAlgorithmException {
        try {
//            System.out.println("Try to upload image");
//            System.out.println(event.getFile().getFileName());
//            String filePath = "C:\\IS3102Project\\IS3102Project-war\\web\\images";
//            File result = new File(filePath + System.getProperty("file.separator") + event.getFile().getFileName().replace(" ", "_"));
//            FileOutputStream out = new FileOutputStream(result);
//            int a;
//            int BUFFER_SIZE = 8192;
//            byte[] buffer = new byte[BUFFER_SIZE];
//            InputStream is = event.getFile().getInputstream();
//            while (true) {
//                a = is.read(buffer);
//                if (a < 0) {
//                    break;
//                }
//                out.write(buffer, 0, a);
//                out.flush();
//            }
//
//            selectedMeal.setImageLink(event.getFile().getFileName().replace(" ", "_"));
//            out.close();
//            is.close();
//
//            FacesMessage msg = new FacesMessage("Succesful " + event.getFile().getFileName() + " is uploaded.");
//            FacesContext.getCurrentInstance().addMessage(null, msg);
            selectedStakeholder = (StakeholderEntity) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholder");
            selectedBranch = (FnBBranchEntity) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedBranch");
            System.out.println("Try to upload image");
            System.out.println(event.getFile().getFileName());
            String strDirectory = selectedStakeholder.getUsername();
            String branchDirectory = selectedBranch.getId().toString();
            String filePath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/images/upload/media/" + strDirectory + "/" + branchDirectory + "/product/");
            //String filePath = "C:\\IS3102Project\\IS3102Project-war\\web\\images";
            File file = new File(filePath);
            if (!file.exists()) {
                if ((new File(filePath)).mkdirs()) {
                    System.out.println("Directory: " + strDirectory + " created");
                }
            }

            File result = new File(filePath + System.getProperty("file.separator") + event.getFile().getFileName().replace(" ", "_"));
            System.out.println("result: " + result + " created");
            FileOutputStream out = new FileOutputStream(result);
            int a;
            int BUFFER_SIZE = 8192;
            byte[] buffer = new byte[BUFFER_SIZE];
            InputStream is = event.getFile().getInputstream();
            while (true) {
                a = is.read(buffer);
                if (a < 0) {
                    break;
                }
                out.write(buffer, 0, a);
                out.flush();
            }
            out.close();
            is.close();

            FacesMessage msg = new FacesMessage("Succesful " + event.getFile().getFileName() + " is uploaded.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void handleImageUploadMeal(FileUploadEvent event) throws AbortProcessingException, NoSuchAlgorithmException {

        handleImageUpload(event);
        System.out.println(event.getFile().getFileName());
        selectedMeal.setImageLink("upload/media/" + selectedStakeholder.getUsername() + "/" + selectedBranch.getId().toString() + "/product/" + event.getFile().getFileName().replace(" ", "_"));
        System.out.println(selectedMeal.getImageLink());
    }

    public void handleImageUploadSetMeal(FileUploadEvent event) throws AbortProcessingException, NoSuchAlgorithmException {

        handleImageUpload(event);
        selectedSetMeal.setImageLink("upload/media/" + selectedStakeholder.getUsername() + "/" + selectedBranch.getId().toString() + "/product/" + event.getFile().getFileName().replace(" ", "_"));
    }

    //
    public void initMealList(PhaseEvent event) throws IOException {
        if (!checkBranch()) {
            return;
        }

        mealList = mealSessionBean.getMealListOfABranch(selectedBranch.getId());

        if (reloadAddSet) {
            selectedSetMeal = new SetMealEntity();
            selectedMeals = null;
            reloadAddSet = false;
        }
    }

    public void initSetMealList(PhaseEvent event) throws IOException {
        if (!checkBranch()) {
            return;
        }
        setMealList = mealSessionBean.getSetMealListOfABranch(selectedBranch.getId());
    }

    public void initProductList(PhaseEvent event) throws IOException {
        if (!checkBranch()) {
            return;
        }

        mealList = mealSessionBean.getMealListOfABranch(selectedBranch.getId());
        setMealList = mealSessionBean.getSetMealListOfABranch(selectedBranch.getId());
    }

    public List<MealEntity> getMealList() {
        return mealList;
    }

    public String onRowSelectNavigateMeal(SelectEvent event) {
        selectedProduct = selectedMeal;
        reloadAddMeal = true;
        reloadPriceControl = true;
        reloadProductAvail = true;
        return "StakeholderUpdateSelectedMeal?faces-redirect=true";
    }

    public String onRowSelectNavigateSetMeal(SelectEvent event) {
        selectedProduct = selectedSetMeal;
        reloadAddSet = true;
        reloadPriceControl = true;
        reloadProductAvail = true;
        return "StakeholderUpdateSelectedSetMeal?faces-redirect=true";
    }

    //update Meal and Set Meal
    public void updateMeal(ActionEvent event) {
        mealSessionBean.updateMeal(selectedMeal);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
                FacesMessage.SEVERITY_INFO, "Edit Successfully", ""));
    }

    public void updateSetMeal(ActionEvent event) {
        mealSessionBean.updateSetMeal(selectedSetMeal, getSelectedMeals_update());
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
                FacesMessage.SEVERITY_INFO, "Edit Successfully", ""));
    }

    public void deleteMeal(ActionEvent event) {
        System.out.println("1");
        System.out.println(selectedMeal.getReservedMeal().size());
        try {
            System.out.println("2");
            mealSessionBean.deleteMeal(selectedMeal);
            setStatusMessage("Meal deleted Successfully");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Delete Meal Result: " + getStatusMessage() + " (Meal deleted is " + selectedMeal.getName() + ")", ""));

            mealList = mealSessionBean.getMealListOfABranch(selectedBranch.getId());
            selectedMeal = null;
        } catch (DeleteProductException del) {
            System.out.println("3");
            setStatusMessage("Meal Exception: ");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Delete Meal Result: " + getStatusMessage() + del.getMessage(), ""));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        selectedMeal = new MealEntity();
    }

    public void deleteSetMeal(ActionEvent event) {
        try {
            System.out.println("1");
            mealSessionBean.deleteSetMeal(selectedSetMeal);
            System.out.println("2");
            setStatusMessage("Set Meal deleted Successfully");
            setMealList = mealSessionBean.getSetMealListOfABranch(selectedBranch.getId());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Delete Set Meal Result: " + getStatusMessage() + " (Set Meal deleted is " + selectedSetMeal.getName() + ")", ""));
            selectedSetMeal = null;
        } catch (DeleteProductException del) {
            setStatusMessage("Set Meal Exception: ");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Delete Set Meal Result: " + getStatusMessage() + del.getMessage(), ""));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        selectedSetMeal = new SetMealEntity();
    }
    //getter and setter

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public MealSessionBean getMealSessionBean() {
        return mealSessionBean;
    }

    public void setMealSessionBean(MealSessionBean mealSessionBean) {
        this.mealSessionBean = mealSessionBean;
    }

    public void setMealList(List<MealEntity> mealList) {
        this.mealList = mealList;
    }

    public MealEntity getSelectedMeal() {
        return selectedMeal;
    }

    public void setSelectedMeal(MealEntity selectedMeal) {
        this.selectedMeal = selectedMeal;
    }

    public MealEntity[] getSelectedMeals() {
        return selectedMeals;
    }

    public void setSelectedMeals(MealEntity[] selectedMeals) {
        this.selectedMeals = selectedMeals;
    }

    public SetMealEntity getSelectedSetMeal() {
        return selectedSetMeal;
    }

    public void setSelectedSetMeal(SetMealEntity selectedSetMeal) {
        this.selectedSetMeal = selectedSetMeal;
    }

    public List<SetMealEntity> getSetMealList() {
        return setMealList;
    }

    public void setSetMealList(List<SetMealEntity> setMealList) {
        this.setMealList = setMealList;
    }

    public List<FoodTypeEntity> getFoodTypes() {
        return foodTypes;
    }

    public void setFoodTypes(List<FoodTypeEntity> foodTypes) {
        this.setFoodTypes(foodTypes);
    }

    public Long getOption() {
        return option;
    }

    public void setOption(Long option) {
        this.option = option;
    }

    public List getOptions() {
        return options;
    }

    public void setOptions(List options) {
        this.options = options;
    }

    public MealEntity[] getSelectedMeals_update() {
        return selectedMeals_update;
    }

    public void setSelectedMeals_update(MealEntity[] selectedMeals_update) {
        this.selectedMeals_update = selectedMeals_update;
    }

    public boolean isReload() {
        return reload;
    }

    public void setReload(boolean reload) {
        this.reload = reload;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public List<MealEntity> getMealOfBranch() {
        return mealOfBranch;
    }

    public void setMealOfBranch(List<MealEntity> mealOfBranch) {
        this.mealOfBranch = mealOfBranch;
    }

    public boolean checkStakeholder() throws IOException {
        StakeholderEntity stakeholder = (StakeholderEntity) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholder");
        if (stakeholder == null || stakeholder.getId() == null) {
            FacesContext.getCurrentInstance().getExternalContext().redirect("StakeholderLogin.xhtml");
            return false;
        }
        return true;
    }

    public boolean checkBranch() throws IOException {
        if (!checkStakeholder()) {
            return false;
        }
        selectedBranch = (FnBBranchEntity) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedBranch");
        if (selectedBranch == null || selectedBranch.getId() == null) {
            FacesContext.getCurrentInstance().getExternalContext().redirect("StakeholderViewAllBranch.xhtml");
            return false;
        }
        return true;
    }

    //Product Manager Bean
    public void addPriceControl(ActionEvent event) {
        if (priceControl.getStartTime() >= priceControl.getEndTime()) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, "Add Unsuccessfully: start time must be smaller than end time", ""));
            return;
        }

        try {
            productSessionBean.addPriceControl(selectedProduct.getId(), priceControl);
            priceControl = new PriceControlEntity();
            reloadPriceControl = true;
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_INFO, "Add Successfully", ""));
        } catch (PriceControlException ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, "Add Unsuccessfully: " + ex.getMessage(), ""));
        }
    }

    public void removePriceControl(long id) {
        productSessionBean.removePriceControl(id);
        reloadPriceControl = true;
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
                FacesMessage.SEVERITY_INFO, "Remove Successfully", ""));
    }

    public void editPriceControl(RowEditEvent event) {
        reloadPriceControl = true;
        PriceControlEntity temp = (PriceControlEntity) event.getObject();
        if (temp.getStartTime() >= temp.getEndTime()) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, "Edit Unsuccessfully: Start time must be smaller than end time", ""));
            return;
        }

        try {
            productSessionBean.editPriceControl(temp);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_INFO, "Edit Successfully", ""));

        } catch (PriceControlException ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, "Edit Unsuccessfully: " + ex.getMessage(), ""));
        }
    }

    public void editAvail(ActionEvent event) {
        productSessionBean.editAvail(selectedProduct, selectedAvail);
        reloadProductAvail = true;
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
                FacesMessage.SEVERITY_INFO, "Edit Successfully", ""));
    }

    public void initPriceControl() throws IOException {
        if (reloadPriceControl) {
            listOfPriceControl = productSessionBean.getListOfPriceControl(selectedProduct.getId());
            Collections.sort(listOfPriceControl, new PriceControlEntity());
            System.out.println("Reload Price COntrol");
            reloadPriceControl = false;

            if (reloadProductAvail) {
                selectedAvail = productSessionBean.retrieveAvailListByProductId(selectedProduct.getId());
                listOfAvail = productSessionBean.retrieveAllAvailList();
                System.out.println("Reload Product Avail");
                reloadProductAvail = false;
            }
        }
    }

    public void selectProduct(SelectEvent event) {
        selectedProduct = (ProductEntity) event.getObject();
        reloadPriceControl = true;
        reloadProductAvail = true;
        reloadUpdateSetMeal = true;
    }

    public ProductEntity getSelectedProduct() {
        return selectedProduct;
    }

    public void setSelectedProduct(ProductEntity selectedProduct) {
        this.selectedProduct = selectedProduct;
    }

    public List<PriceControlEntity> getListOfPriceControl() {
        return listOfPriceControl;
    }

    public void setListOfPriceControl(List<PriceControlEntity> listOfPriceControl) {
        this.listOfPriceControl = listOfPriceControl;
    }

    public PriceControlEntity getPriceControl() {
        return priceControl;
    }

    public void setPriceControl(PriceControlEntity priceControl) {
        this.priceControl = priceControl;
    }

    public List getDayOfWeek() {
        return dayOfWeek;
    }

    public void setDayOfWeek(List dayOfWeek) {
        this.dayOfWeek = dayOfWeek;
    }

    public List getTimeOfDay() {
        return timeOfDay;
    }

    public void setTimeOfDay(List timeOfDay) {
        this.timeOfDay = timeOfDay;
    }

    public PriceControlEntity getSelectedPriceControl() {
        return selectedPriceControl;
    }

    public void setSelectedPriceControl(PriceControlEntity selectedPriceControl) {
        this.selectedPriceControl = selectedPriceControl;
    }

    public ProductAvailabilityEntity[] getSelectedAvail() {
        return selectedAvail;
    }

    public void setSelectedAvail(ProductAvailabilityEntity[] selectedAvail) {
        this.selectedAvail = selectedAvail;
    }

    public List<ProductAvailabilityEntity> getListOfAvail() {
        return listOfAvail;
    }

    public void setListOfAvail(List<ProductAvailabilityEntity> listOfAvail) {
        this.listOfAvail = listOfAvail;
    }

    public List<FnBProductEntity> getListOfProduct() {
        return listOfProduct;
    }

    public void setListOfProduct(List<FnBProductEntity> listOfProduct) {
        this.listOfProduct = listOfProduct;
    }

    public boolean checkProduct() throws IOException {
        if (!checkBranch()) {
            return false;
        }

        HttpServletRequest servletRequest = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String productType = servletRequest.getRequestURI();

        if (selectedProduct == null || selectedProduct.getId() == null) {
            String serverName = ctx.getExternalContext().getRequestServerName();
            int serverPort = (Integer) ctx.getExternalContext().getSessionMap().get("serverPort");
            String contextPath = ctx.getExternalContext().getRequestContextPath();
            if (productType.indexOf("SetMeal") != - 1) {

                ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/StakeholderViewSetMeals.xhtml");
            } else {
                ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/StakeholderViewMeals.xhtml");
            }
            return false;
        }

        if (productType.indexOf("SetMeal") != - 1 && selectedProduct instanceof MealEntity) {
            String serverName = ctx.getExternalContext().getRequestServerName();
            int serverPort = (Integer) ctx.getExternalContext().getSessionMap().get("serverPort");
            String contextPath = ctx.getExternalContext().getRequestContextPath();
            ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/StakeholderViewSetMeals.xhtml");
            return false;
        }

        if (productType.indexOf("SetMeal") == -1 && selectedProduct instanceof SetMealEntity) {
  String serverName = ctx.getExternalContext().getRequestServerName();
            int serverPort = (Integer) ctx.getExternalContext().getSessionMap().get("serverPort");
            String contextPath = ctx.getExternalContext().getRequestContextPath();
            ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/stakeholder/StakeholderViewMeals.xhtml");
            return false;
        }

        return true;
    }
}
